<div class="layui-fluid layui-anim febs-anim" id="businesses-group" lay-title="学生组">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="group-table-form">
                        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                            <legend style="font-size: 15px; font-weight: bolder;">批量查询</legend>
                        </fieldset>
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">批次</label>
                                        <div class="layui-input-inline">
                                            <select name="batchId" lay-filter="sysBunk" id="group-cboBatch"
                                                    lay-search="" lay-verify="select">
                                                <option value="">请选择</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">招生老师</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="userId" id="group-cboUser"
                                                   lay-filter="group-cboUser" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">组名</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="groupNo" lay-verify="txt" maxlength="10"
                                                   autocomplete="off" class="layui-input"></div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action"
                                     id="group-query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain table-action"
                                     id="group-reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                            </div>
                        </div>
                        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                            <div class="layui-row">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action"
                                     id="group-addGroup">
                                    <i class="layui-icon layui-icon-plus-circle">新建组</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain table-action"
                                     id="group-export">
                                    <i class="layui-icon layui-icon-export">导出</i>
                                </div>
                            </div>
                        </blockquote>
                    </form>
                    <table lay-filter="groupTable" lay-data="{id: 'groupTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="group-option">
    <a lay-event="add" title="添加学生"><i class="layui-icon febs-edit-area febs-blue">&#xe7cb;</i></a>
    <a lay-event="del" title="删除学生组"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script type="text/javascript" src="module/treeSelect/treeSelect.js"></script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'treeSelect', 'form', 'table', 'febs'], function () {
        let $ = layui.jquery,
            febs = layui.febs,
            form = layui.form,
            treeSelect = layui.treeSelect,
            table = layui.table,
            $view = $('#businesses-group'),
            $query = $view.find('#group-query'),
            $addGroup = $view.find('#group-addGroup'),
            $export = $view.find('#group-export'),
            $reset = $view.find('#group-reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createDate', type: null},
            tableIns;

        form.render();

        initTable();

        $(function () {
            $.ajax({
                url: ctx + 'college/batchSelect',
                success: function (res) {
                    for (let i = 0; i < res.data.length; i++) {
                        const batch = res.data[i];
                        $("#group-cboBatch").append("<option value='" + batch.id + "'>" + batch.batchName + "</option>");
                    }
                    form.render("select");
                }
            })
        });

        $(".layui-input").keyup(function (e) {
            if (e.which === 13) {
                $query.click();
            }
        })
        table.on('tool(groupTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'add') {
                febs.modal.view('添加学生', 'group/grouping/' + data.id, {
                    area: $(window).width() <= 750 ? ['95%', '85%'] : ['1300px', '730px']
                });
            }
            if (layEvent === 'del') {
                if (data.stuNum == 0) {
                    febs.modal.confirm('删除学生组', '确定删除该组？', function () {
                        deleteGroup(data.id);
                    });
                } else {
                    febs.alert.warn('学生组人数大于0，不能删除');
                }
            }
        });

        table.on('sort(groupTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $addGroup.on('click', function () {
            febs.modal.open('新建组', 'group/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                yes: function (index, layero) {
                    $('#group-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                }
            });
        });

        $export.on('click', function () {
            let params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
            params.pageNum = $view.find(".layui-laypage-em").next().html();
            const d = new Date();
            const date = d.getFullYear()+'-'+ d.getMonth()+'-'+ d.getDay()+'-'+ d.getHours()+'-'+ d.getMinutes()+'-'+ d.getSeconds();
            let time = '学生组信息表' + date + '.xlsx';
            febs.download(ctx + 'group/excel',params, time);
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            tableIns.reload({where: getQueryParams(true), page: {curr: 1}});
        });
        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'groupTable',
                url: ctx + 'group/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: '编号'},
                    {field: 'batchName', title: '批次', minWidth: 100},
                    {field: 'groupNo', title: '组名'},
                    {field: 'userName', title: '招生老师'},
                    {field: 'stuNum', title: '学生数量'},
                    {field: 'createUserName', title: '创建人', minWidth: 100},
                    {field: 'createTime', title: '创建时间', minWidth: 120, sort: true},
                    {field: 'remark', title: '备注'},
                    {title: '操作', toolbar: '#group-option', minWidth: 140}
                ]]
            });
        }

        //构建用户树
        treeSelect.render({
            // 选择器
            elem: '#group-cboUser',
            // 数据
            data: ctx + 'transferApplication/userDeptTree',
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '请选择',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            style: {
                folder: { // 父节点图标
                    enable: true // 是否开启：true/false
                },
                line: {
                    enable: true
                }
            },
            // 点击回调
            click: function (d) {
                $("#group-cboUser").val(d.current.id);
            },
            // 加载完成后的回调函数
            success: function (d) {
            }
        });

        function getQueryParams(flag) {
            let params = $searchForm.serializeJson();
            return params;
        }

        function deleteGroup(groupIds) {
            febs.get(ctx + 'group/delete/' + groupIds, null, function () {
                febs.alert.success('删除学生组成功');
                $query.click();
            });
        }
    })
</script>